#!C:\Python26

from pluginManage import Plugin
import re
import os
import urlparse

import sys
import time
import logging

import traceback

import urllib
import webFunctions

class SJWalker(Plugin):

	#Configuration Variables - Loaded when plugin is selected
	capabilities		=	"wScraperDisabled"									#Used for detecting if any random .py file is a plugin file
	version			=	1.3										#Used for detecting available configuration options
	pluginName		=	"DrunkDuckScraper"								#Plugin Name variable
	siteName		=	"Drunk Duck Site Scraper"								#Value for populating "Art Site" dropbox
	nameSource		=	[]										#namesource value passed to plugin
	nameSourceEN		=	1										#Determines if Name Source input box is enabled (Boolean, 0-1)
	artistListEN		=	1										#Determines if Artist List input box is enabled (Boolean, 0-1)
	overwriteModes		=	["Check Folders", "Check Files", "Overwrite Files"]						#Determines if overwrite mode dropbox is enabled for plugin
	retrievalHandles	=	1										#How may http retrieval handles app wants

	DLFolder		=	"Drunk Duck"
	logFileName		=	"LogFile.txt"

	#V1.1 variables:

	aNameLabeltxt		=	"Disabled"
	aListLabeltxt		=	"Disabled"
	#V1.2 variables:

	cookieCapable = 1

	#V1.3 variables:
	nameSource			=	["Comic List", "All Comics"]				#namesource value passed to plugin (Stuffed at runtime)
	nameSourceEN			=	1				#Determines if Name Source input box is enabled (Boolean, 0-1)


	instructionText		=	\
	'''Blag


	'''

	pageFuncList = []
	waitTimer = .1

	def checkCookie(self, cookiejar):

		userID = re.search("<Cookie dd_id=\d*? for \.drunkduck\.com/>", "%s" % cookiejar)
		sessionID = re.search("<Cookie dd_s=[a-z0-9]*? for \.drunkduck\.com/>", "%s" % cookiejar)
		timeID = re.search("<Cookie ttime=\d*? for \.drunkduck\.com/>", "%s" % cookiejar)
		if userID and sessionID:
			return "Have DD Cookies:\n	%s\n	%s\n	%s" % (userID.group(0), sessionID.group(0), timeID.group(0))

		return "Do not have DD login Cookies"

		return "Do not have DD login Cookies"

	def getCookie(self, userName, password, opener):


		params = urllib.urlencode({"un" : userName, "pw" : password, })
		page = opener.open('http://www.drunkduck.com/index.php', params)
		pagetext = page.read()
		page.close()
		#print pagetext
		if re.search('<a href="http://user\.drunkduck\.com/[a-z0-9_\-]*?"><img src="http://images\.drunkduck\.com/process/user_\d*?\." height="50" width="50" border="0">', pagetext):
			return "Logged In"
		else:
			return "Login Failed"



	def __init__(self):
		self.log = logging.getLogger("Main.Plugin")
		print "Initing"


	def getImage(self, imgURL, counter, config):


		type = re.search("\.(\w\w\w)$", imgURL)
		if type == None:
			type = "jpg"
		else:
			try:
				type = type.group(1)
			except:
				type = "jpg"
		imgName = ("img%7.1f.%s" % (counter, type)).replace(" ","0")

		imgName = os.path.join(self.DLFolder, config.currComicName, imgName)


		self.log.info("Getting : '%s', renaming to: '%s'" % (imgURL, imgName))



		if os.access(imgName, os.F_OK):
			if os.path.getsize(imgName) < 1000:
				undersize = True
				self.log.info("Existimg Image smaller than 1K - Redownloading")
			else:
				undersize = False
		else:
			undersize = False

		if config.ovwMode == "Overwrite Files" or not os.access(imgName, os.W_OK) or undersize:
			imgPtr = open(imgName, "wb")
			imgHandle = self.getHandles.getpage(imgURL)

			#print "Got Image"

			if imgHandle == "Failed":
				self.log.error("Error : Could not get page 1. Failed")
				self.log.error("%s" % (config.currComicName))
				self.log.error("%s" % (imgURL))
			else:
				imgPtr.write(imgHandle)
			#print "Wrote Image"
			imgPtr.close()
		else:
			self.log.info("Already have Comic : %s " % imgURL)



	def getComic(self, comicRootPage, config):
		config.currComicName = comicRootPage.rsplit("/", 1)[1]

		comicFolderPath = os.path.join(self.DLFolder, config.currComicName)

		if config.ovwMode == "Check Folders" and os.access(comicFolderPath, os.W_OK):
			self.log.warning("Comic folder %s already exists - Skipping" % comicFolderPath)
			return 0


		if not os.access(comicFolderPath, os.W_OK):
			try:
				os.mkdir(os.path.join(self.DLFolder, config.currComicName))
				self.log.info("Created folder "+os.path.join(self.DLFolder, config.currComicName))
				#self.log.info("Beginning Download")
			except:
				self.log.info("Cannot Make working directory (%s/. Do you have write Permissions? %s" % (os.path.join(self.DLFolder, config.currComicName), sys.exc_info()[0]))
				return 0


		self.log.info("Getting Archive Root")

		currPage = self.getHandles.getpage(comicRootPage)				#Get webpage
		if currPage == "Failed":
			self.log.info("Error : Could not get page %s. Failed" % comicRootPage)
			self.log.info("%s" % (comicRootPage))
			return 0


		rootPageRESimple = re.compile("<A HREF='([a-zA-Z0-9\.\-/\?=@+_#:]*?)' TITLE='The first page!'><IMG SRC='http://www\.drunkduck\.com/.*?' BORDER='0'></A>", re.IGNORECASE)
		rootPageREMouseover = re.compile(" <li><a href=\"([a-zA-Z0-9\.\-/\?=@+_#:]*?)\">First</a></li>", re.IGNORECASE)
		rootPageREMouseover2 = re.compile("<td align=\"\w*?\" height=\"\d*?\" width=\"\d*?%\"><a href=\"(/.*?/index\.php\?p=\d*?)\" onMouseOver=\"document\.First\.src='.*?';\" onMouseOut=\"document\.First\.src='.*?';\"><img src=\".*?\" name=\"First\"></a></td>", re.IGNORECASE)
		reL = [rootPageRESimple, rootPageREMouseover, rootPageREMouseover2]
		startLink = []
		for rootRe in reL:
			startLink = rootRe.search(currPage)
			if startLink:
				startLink = startLink.group(1)
			if startLink != None:
				break
		print "StartLink - ", startLink

		if startLink == []:
			#Looks like there's an entry buffer page - Try any link that could go to another comic page
			lastCaseRE = "http://www.drunkduck.com/%s/\?p=\d*?" % config.currComicName

			lastCaseRE = re.compile(lastCaseRE, re.IGNORECASE)
			#TODO Make work (hahaha)
			self.log.error("Cannot find root link on comic page")
			self.log.error(comicRootPage)
			return

#Ok, now we have the start page for the comic.



		nextPageRESimple = re.compile("<A HREF='([a-zA-Z0-9\.\-/\?=@+_#:]*?)' TITLE='The next page!'><IMG SRC='[a-zA-Z0-9\.\-/\?=@+_#:]*?' BORDER='0'></A>", re.IGNORECASE)
		nextPageREMouseover = re.compile("<td align=\"\w*?\" height=\"\d*?\" width=\"\d*?%\"><a href=\"(/\w*?/index\.php\?p=\d*?)\" onMouseOver=\"document\.Next\.src='[a-zA-Z0-9\.\-/\?=@+_#:]*?';\" onMouseOut=\"document\.Next\.src='[a-zA-Z0-9\.\-/\?=@+_#:]*?';\"><img src=\"[a-zA-Z0-9\.\-/\?=@+_#:]*?\" name=\"Next\"></a></td>", re.IGNORECASE)
		reL = [nextPageRESimple, nextPageREMouseover]

		imgRESimple = re.compile("<IMG SRC='(http://comics\.drunkduck\.com/\w*?/pages/[a-zA-Z0-9]*?\.\w*?)'>", re.IGNORECASE)
		reI = [imgRESimple]

		nextLink = urlparse.urljoin(comicRootPage, startLink)

		pageCounter = 0

		while nextLink:
			try:
				#x = 1/0					#Test exception handling
				#print nextLink
				currPage = self.getHandles.getpage(nextLink)				#Get webpage
				if currPage == "Failed":
					self.log.error("Error : Could not get page %s. Failed" % nextLink)
					self.log.error("%s" % (nextLink))
					return 0




				nextLink = []
				imageLinks = []
				for nextRe in reL:
					currLink = nextRe.search(currPage)


					if currLink:
						currLink = currLink.group(1)
						self.log.info("currLink - %s" % currLink)
					if currLink != None:
						#print nextLink
						currLink = urlparse.urljoin(comicRootPage, currLink)
						self.log.info("Parsed Link - %s " % currLink)
						break
				tempCounter = pageCounter

				for nextRe in reI:
					imageLinks = nextRe.findall(currPage)
					self.log.info("ImageLink - %s" % imageLinks)
					if imageLinks:
						for imgLink in imageLinks:
							tempCounter += 0.1

							self.getImage(imgLink, tempCounter, config)
							self.log.info(imgLink)


				if currLink == nextLink:
					self.log.info("At end of Archive")
					return
				else:
					nextLink = currLink

				pageCounter += 1
				self.progBars.pulseSubBar()

				time.sleep(self.waitTimer)


			except:
				self.log.error("Critical error on %s comic?", comicRootPage)
				exec_info = sys.exc_info()
				self.log.error(exec_info)
				self.log.error(traceback.format_exc(10))

				raise Exception("FailedComic")


		self.log.info("At end of Archive")
		return

	'''
	<td align="center" height="50" width="25%"><a href="/SHELL/index.php?p=698723" onMouseOver="document.Prev.src='http://www.drunkduck.com/SHELL/gfx/prev.gif';" onMouseOut="document.Prev.src='http://www.drunkduck.com/SHELL/gfx/prev.jpg';"><img src="http://www.drunkduck.com/SHELL/gfx/prev.jpg" name="Prev"></a></td>
	'''


	def getComicList(self):
		pageNum = 1
		flag = True

		links = []

		comicHomeRE = re.compile("<a href='(http://www\.drunkduck\.com/.*?)'><img src=\".*?\" border=\"0\" onMouseOver=\"LinkTip\.show\('\d*?', this\);\" onMouseOut=\"LinkTip\.hide\(\);\"></a>")
		while flag:
			pageURL = "http://www.drunkduck.com/search.php?amtPerPage=47&p=%d" % pageNum
			self.log.info(pageURL)
			currPage = self.getHandles.getpage(pageURL)				#Get webpage
			if currPage == "Failed":
				self.log.info("Error : Could not get page %s. Failed" % pageNum)
				self.log.info("%s" % (pageURL))
				return 0



			tempLinks = comicHomeRE.findall(currPage)
			if len(tempLinks) == 0:
				flag = False
			else:
				for link in tempLinks:
					links.append(link)
			pageNum += 1
			time.sleep(self.waitTimer)
		return links


	def main(self, config, getHandles):											 #Main Function Call - Uses other functions
		self.log.info(config)
		self.log.info(getHandles)

		self.getHandles = getHandles

		self.progBars = webFunctions.ProgressBars()

		if not os.access(os.path.join(self.DLFolder), os.W_OK):
			try:
				os.mkdir(os.path.join(self.DLFolder))
				self.log.info("Created folder "+os.path.join(self.DLFolder))
				#self.log.info("Beginning Download")
			except:
				self.log.info("Cannot Make working directory (%s/. Do you have write Permissions? %s" % (os.path.join(self.DLFolder), sys.exc_info()[0]))
				return 0



		logFile = os.path.join(self.DLFolder, self.logFileName)		#Open local Logfile
		#print logFile
		config.localLog = open(logFile, "a")

		comicPageList = self.getComicList()
		self.log.info("Found %s Comics" % len(comicPageList))


		self.progBars.setMainBarLen(len(comicPageList))
		self.progBars.setMainBar(0)

		for comicPage in comicPageList:
			try:
				self.getComic(comicPage, config)
				self.progBars.incMainBar()
			except:
				self.log.info("FAILED On comic %s", comicPage)


		self.log.info("Done")
		return 1


